package com.px.auditRecord.service.impl;

import java.util.List;

import cn.hutool.core.convert.Convert;
import com.px.auditRecord.domain.ForRecordDrone;
import com.px.auditRecord.domain.ForRecordUser;
import com.px.auditRecord.mapper.ForRecordDroneMapper;
import com.px.auditRecord.mapper.ForRecordUserMapper;
import com.px.common.constant.HttpStatus;
import com.px.common.exception.ServiceException;
import com.px.common.utils.DateUtils;
import com.px.common.utils.SecurityUtils;
import com.px.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.px.auditRecord.mapper.AuditRecordDroneMapper;
import com.px.auditRecord.domain.AuditRecordDrone;
import com.px.auditRecord.service.IAuditRecordDroneService;

/**
 * 设备审核记录Service业务层处理
 * 
 * @author WRJ
 * @date 2024-08
 */
@Service
public class AuditRecordDroneServiceImpl implements IAuditRecordDroneService 
{
    @Autowired
    private AuditRecordDroneMapper auditRecordDroneMapper;

    @Autowired
    private ForRecordDroneMapper forRecordDroneMapper;

    @Autowired
    private ForRecordUserMapper forRecordUserMapper;

    /**
     * 查询设备审核记录
     * 
     * @param id 设备审核记录主键
     * @return 设备审核记录
     */
    @Override
    public AuditRecordDrone selectAuditRecordDroneById(Integer id)
    {
        return auditRecordDroneMapper.selectAuditRecordDroneById(id);
    }

    /**
     * 查询设备审核记录列表
     * 
     * @param auditRecordDrone 设备审核记录
     * @return 设备审核记录
     */
    @Override
    public List<AuditRecordDrone> selectAuditRecordDroneList(AuditRecordDrone auditRecordDrone)
    {
        List<AuditRecordDrone> list = auditRecordDroneMapper.selectAuditRecordDroneList(auditRecordDrone);
        list.forEach(ard -> {
            ard.setForRecordUser(forRecordUserMapper.selectForRecordUserByUserId(Integer.valueOf(ard.getCreateBy())));
        });
        return list;
    }

    /**
     * 新增设备审核记录
     * 
     * @param auditRecordDrone 设备审核记录
     * @return 结果
     */
    @Override
    public int insertAuditRecordDrone(AuditRecordDrone auditRecordDrone)
    {
        return auditRecordDroneMapper.insertAuditRecordDrone(auditRecordDrone);
    }

    /**
     * 修改设备审核记录
     * 
     * @param auditRecordDrone 设备审核记录
     * @return 结果
     */
    @Override
    public int updateAuditRecordDrone(AuditRecordDrone auditRecordDrone)
    {
        if("1".equals(auditRecordDrone.getAuditState())){
            //先查询
            AuditRecordDrone ard = auditRecordDroneMapper.selectAuditRecordDroneById(auditRecordDrone.getId());
            ForRecordDrone frd = Convert.convert(ForRecordDrone.class, ard);
            frd.setAuditRecordDroneId(auditRecordDrone.getId());
            frd.setFlightUserId(ard.getCreateBy());
            forRecordDroneMapper.insertForRecordDrone(frd);
        }
        return auditRecordDroneMapper.updateAuditRecordDrone(auditRecordDrone);
    }

    /**
     * 批量删除设备审核记录
     * 
     * @param ids 需要删除的设备审核记录主键
     * @return 结果
     */
    @Override
    public int deleteAuditRecordDroneByIds(Integer[] ids)
    {
        return auditRecordDroneMapper.deleteAuditRecordDroneByIds(ids);
    }

    /**
     * 删除设备审核记录信息
     * 
     * @param id 设备审核记录主键
     * @return 结果
     */
    @Override
    public int deleteAuditRecordDroneById(Integer id)
    {
        return auditRecordDroneMapper.deleteAuditRecordDroneById(id);
    }

    @Override
    public int flightDroneAdd(AuditRecordDrone auditRecordDrone) {
        /***
         * 0、查询是否有绑定人员 - 没绑定直接返回
         *
         * 1、查询是否有已备案成功的设备
         * 2、查询是否有审核中的设备
         */
        ForRecordUser fru = forRecordUserMapper.selectForRecordUserByUserId(SecurityUtils.getUserId().intValue());
        if(StringUtils.isNull(fru)){
            throw new ServiceException("该账号暂未备案人员信息", HttpStatus.CREATED);
        }
        ForRecordDrone frd= forRecordDroneMapper.selectForRecordDroneBySn(auditRecordDrone.getSn());
        if(StringUtils.isNotNull(frd)){
            throw new ServiceException("该设备已被绑定，无法进行审核", HttpStatus.CREATED);
        }
        AuditRecordDrone ard = auditRecordDroneMapper.selectAuditRecordDroneBySn(auditRecordDrone.getSn());
        if(StringUtils.isNotNull(ard)){
            if(ard.getAuditState().equals("0"))
                throw new ServiceException("该设备正在审核中，请耐心等待", HttpStatus.WARN);
            if(ard.getAuditState().equals("1"))
                throw new ServiceException("该设备已审核通过，请刷新", HttpStatus.WARN);
        }
        auditRecordDrone.setCreateTime(DateUtils.getNowDate());
        //查询身份证放进去
        auditRecordDrone.setIdcard(fru.getIdcard());
        auditRecordDrone.setAuditState("0");
        return auditRecordDroneMapper.insertAuditRecordDrone(auditRecordDrone);
    }

    @Override
    public List<AuditRecordDrone> selectFlightList(AuditRecordDrone auditRecordDrone) {
        ForRecordUser fru = forRecordUserMapper.selectForRecordUserByUserId(SecurityUtils.getUserId().intValue());
        if(StringUtils.isNull(fru)){
            throw new ServiceException("该账号暂未备案人员信息", HttpStatus.CREATED);
        }
        auditRecordDrone.setIdcard(fru.getIdcard());
        return auditRecordDroneMapper.selectAuditRecordDroneList(auditRecordDrone);
    }
}
